package com.atomdayl.domain.therecipe.utils;

import android.os.Environment;
import android.util.Log;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;

/**
 * Created by David on 2016/6/4.
 */
public class LogUtils {

    private static String log_fullpath = "";
    private static String file_name = "";
    private static String rootdir = Environment.getExternalStorageDirectory()
            .getAbsolutePath();
    private static final String file_idle = "/";

    private static final boolean IS_LOG = true;

    public static void d(String tag, String msg) {
        if (IS_LOG) {
            Log.d(tag, msg);
        }
    }

    public static void i(String tag, String msg) {
        if (IS_LOG) {
            Log.i(tag, msg);
        }
    }

    public static void e(String tag, String msg) {

        if (IS_LOG) {
            Log.e(tag, msg);
        }
    }

    public static void w(String tag, String msg) {
        if (IS_LOG) {
            Log.w(tag, msg);
        }
    }

    public static void v(String tag, String msg) {
        if (IS_LOG) {
            Log.v(tag, msg);
        }
    }
    private static boolean hasSdcard() {
        try
        {
            if (!android.os.Environment.getExternalStorageState().equals(
                    android.os.Environment.MEDIA_MOUNTED)) {
                return false;
            }
        }
        catch(Exception ex)
        {
            return false;
        }
        return true;
    }

    /**
     * 写日志到sdcard,
     * @param content
     * @param type
     */
    private static void writeFileLog(String content, String type) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss:SSS");
        String date = sdf.format(new java.util.Date());
        OutputStreamWriter writer = null;
        try {
            writer = new OutputStreamWriter(new FileOutputStream(log_fullpath,true),"gbk");
            writer.write(date + " " + type + " " + content + "\r\n");
        } catch (Exception ex) {

        }
        finally {
            if (writer != null) {
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    /**
     * 字符串编码转换的实现方法
     * @param str  待转换编码的字符串
     * @param newCharset 目标编码
     * @return
     * @throws UnsupportedEncodingException
     */
    public static String changeCharset(String str, String newCharset)
            throws UnsupportedEncodingException {
        if (str != null) {
            //用默认字符编码解码字符串。
            byte[] bs = str.getBytes();
            //用新的字符编码生成字符串
            return new String(bs, newCharset);
        }
        return null;
    }
    /**
     * 字符串编码转换的实现方法
     * @param str  待转换编码的字符串
     * @param oldCharset 原编码
     * @param newCharset 目标编码
     * @return
     * @throws UnsupportedEncodingException
     */
    public static String changeCharset(String str, String oldCharset, String newCharset)
            throws UnsupportedEncodingException {
        if (str != null) {
            //用旧的字符编码解码字符串。解码可能会出现异常。
            byte[] bs = str.getBytes(oldCharset);
            //用新的字符编码生成字符串
            return new String(bs, newCharset);
        }
        return null;
    }
}
